<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="saveemployeeInfo" label-width="120px" style="margin-left: 120px; margin-top:30px" :model="userInfo" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="userInfo.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submit">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- <userDetail /> -->
            <!-- 动态路由写法 -->
            <component :is="page1" />
          </el-tab-pane>

          <el-tab-pane label="岗位信息">
            <!-- <jobDetail /> -->
            <component :is="page2" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
// 根据id找个人信息
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import userDetail from './components/userDetail.vue'
import jobDetail from './components/jobDetail.vue'
export default {
  components: {
    userDetail, jobDetail
  },
  data() {
    return {
      page1: 'userDetail',
      page2: 'jobDetail',
      // 这样可以后面直接通过 this.userId进行获取数据
      userId: this.$route.params.id, // 拿到的是配置路由的时候动态传过来的id
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 2, max: 8, message: '名字长度在2 -8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 4, max: 50, message: '密码长度在4 -20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    async getUserDetailById() {
      // 根据id进行数据的获取
      // this.userInfo = await getUserDetailById(this.userId)
      // 明文密码不应该直接显示出来，在前端没办法直接解密1出来，而且解密出来了数据也不安全
      const res = await getUserDetailById(this.userId)
      this.userInfo = {
        // 展开运算符
        ...res, password: ''
      }
      console.log(res)
    },
    async submit() {
      // 校验表单
      await this.$refs.saveemployeeInfo.validate()
      // 发起请求
      await saveUserDetailById(this.userInfo)
      // 提示用户
      this.$message.success('修改成功')
    }
  }
}
</script>

<style>

</style>
